﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using BLL;
using Model;

public partial class partyinforEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            string strId = Request.QueryString["id"];
            LoadView();
            if (!string.IsNullOrWhiteSpace(strId))
                LoadView(int.Parse(strId));
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
       var list= new BllParty().GetList(c=>true);
       foreach (var item in list)
       {
           this.ddlType.Items.Add(new ListItem(item.name, item.id.ToString()));
       }
        lbl.Text = "党团文章";
        

    }

    private void LoadView(int id)
    {
        hdId.Value = id.ToString();
        btnAdd.Text = "修改";
        BllPartyinfor bll = new BllPartyinfor();
        var obj = bll.GetSingle(id);
        this.txtContent.Text = obj.content;
        this.ddlType.SelectedValue = obj.type.ToString();
        txtName.Text = obj.name;
        hdId.Value = obj.id.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string cmd = btnAdd.Text.Trim();
        BllPartyinfor bll = new BllPartyinfor();
        string name = txtName.Text.Trim();
        string content = txtContent.Text;
        int type = int.Parse(ddlType.SelectedValue);
        if (name == "" )
        {
            Util.UtilWeb.Message(this, Util.KoInfo.FillBlank, null);
            return;

        }

        if (cmd == "添加")
        {
            tb_partyinfor obj = new tb_partyinfor();
          
            obj.name = name;
            obj.content = content;
            obj.type = type;
            obj.time = DateTime.Now;
            if (bll.Add(obj))
            {
                Response.Redirect("partyinforList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
        if (cmd == "修改")
        {
            int id = int.Parse(hdId.Value);
            tb_partyinfor obj = bll.GetSingle(id);
            obj.name = name;
            obj.content =content;
            obj.type = type;
            obj.time = DateTime.Now;
            if (bll.Update(obj))
            {
                Response.Redirect("partyinforList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
    }

}